# -*- coding:utf-8 -*-
# @FileName  :t16.py
# @Time      :2021/7/15 16:03
import base64
import io
import sys

import requests
from lxml import html

from yrx.bisai_yrx.t8t.dalao import SplitQrCode

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')
etree = html.etree


def shibie(q,m):
    split_qrcode = SplitQrCode()
    data = split_qrcode.run(filePath='1.png')
    print(data)
    print(q)
    cout = 0
    s = ''
    for i in q:
        if i not in data:
            if cout < 1:
                cout += 1
            else:
                print('识别失败')
                main(m)
            s += str(dian[data.index('')+1]) + '|'
        elif i in data:
            s += str(dian[data.index(i)+1]) + '|'
    return s

def main(m):
    global num, dian
    kv = {'User-Agent': 'yuanrenxue.project', 'cookie': 'sessionid=iq1x32ozkm0lliquhdkhn19mb25j8axe;'}
    r = requests.get(f'http://match.yuanrenxue.com/api/match/8_verify', headers=kv).json()
    sp = etree.HTML(r['html'])
    listt = sp.xpath('//p/text()')
    with open('1.png', 'wb') as f:
        f.write(base64.b64decode(sp.xpath('//img/@src')[0].replace('data:image/jpeg;base64,', '')))
    par = {
        'page': m,
        'answer': shibie(listt,m),
    }
    r1 = requests.get(f'http://match.yuanrenxue.com/api/match/8', headers=kv, params=par).json()
    print(r1)
    print(par)
    for i in r1['data']:
        sd.append(i['value'])
    for j in sd:
        if j not in num:
            num[j] = 1
        else:
            num[j] = num[j]+1


if __name__ == "__main__":
    num = {}
    sd=[5231,7453,6090,6476,2965,5510,3879,1256,7453,5821,7453,1457,5053,2127,4455,4290,9875,7453,8778,2571,3932,5963,3372,9736,7831,1706,887,9955,4029,3034,9606,3850,4100,2381,8545,2403,9984,7453,3585,7545]
    dian = {
        1: 125,
        2: 165,
        3: 175,
        4: 426,
        5: 496,
        6: 506,
        7: 756,
        8: 765,
        9: 776
    }
    for i in range(5, 6):
        main(i)
        # input('1111')
    print(num)
